#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QHotkey>
#include <QtDebug>
#include <QQuickView>
#include <qwindowmanager.h>

int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

    QGuiApplication app(argc, argv);

    QWindowManager wm;

    const QUrl mainUrl(QStringLiteral("qrc:/main.qml"));
    wm.newWindow("main", mainUrl);

    const QUrl launcherUrl(QStringLiteral("qrc:/Launcher.qml"));
    wm.newWindow("launcher", launcherUrl);


    QQmlApplicationEngine *mainWindow = wm.getWindow("main");
    if (nullptr == mainWindow)
    {
        qDebug() << "get main window failed.";
        QCoreApplication::exit(-1);
    }

    QObject::connect(mainWindow, &QQmlApplicationEngine::objectCreated,
                     &app, [mainUrl](QObject *obj, const QUrl &objUrl) {
        if (!obj && mainUrl == objUrl)
            QCoreApplication::exit(-1);
    }, Qt::QueuedConnection);

    QHotkey hotkey(QKeySequence("alt+space"), true);
    qDebug() << "Is Registered: " << hotkey.isRegistered();

    QQmlApplicationEngine *launcherWindow = wm.getWindow("launcher");
    QObject::connect(&hotkey, &QHotkey::activated, launcherWindow, [&]() {
        QObject *launcherObj = launcherWindow->rootObjects()[0];
        bool visible = launcherObj->property("visible").toBool();
        if (visible)
        {
            qDebug() << "Hotkey Activated - the lancherObj will hide.";
            launcherObj->setProperty("visible", false);
        }
        else
        {
            qDebug() << "Hotkey Activated - the lancherObj will show.";
            launcherObj->setProperty("visible", true);
            launcherObj->setProperty("focus", true);
        }
    });

    return app.exec();
}
